﻿using TMPro;
using UnityEngine;

namespace HEFramework
{
    public static class TrialUISystem
    {
        [EntitySystem]
        public class TrialUIAwakeSystem : AwakeSystem<TrialUI>
        {
            protected override void Awake(TrialUI _self)
            {
                _self.AddChild<UnityObjectComponent>();
            }
        }


        [EntitySystem]
        public class TrialUIUpdateSystem : UpdateSystem<TrialUI>
        {
            protected override void Update(TrialUI _self)
            {
                _self.Update();
            }
        }


        [EntitySystem]
        public class TrialUIDestroySystem : DestroySystem<TrialUI>
        {
            protected override void Destroy(TrialUI _self)
            {
                _self.Close();
            }
        }


        public static void Open(this TrialUI _self)
        {
            var uc = _self.GetSceneChild<UIComponent>();
            if (uc == null)
            {
                return;
            }

            uc.PushUIAsync(eUIFlag.Trial, _ =>
            {
                _self.UIWindowBehaviour = _;
                _self.Bind();
                _self.Update(true);
            });
        }

        public static void Bind(this TrialUI _self)
        {
            if (_self.UIWindowBehaviour == null)
            {
                return;
            }

            var trial = _self.GetSceneChild<Trial>();
            if (trial == null)
            {
                return;
            }

            _self.UIWindowBehaviour.GetButton("Button_Back").RegisterUpEvent(_self.Close);

            for (int i = 0; i < trial.Trials.Count; i++)
            {
                int id = trial.Trials[i].ID;
                _self.UIWindowBehaviour.GetButton($"Button_TrialItem{i}").RegisterUpEvent(() => { _self.Select(id); });
            }
        }

        public static void Update(this TrialUI _self, bool _isNew = false)
        {
            if (_self.UIWindowBehaviour == null || _self.UIWindowBehaviour.State == 0)
            {
                return;
            }

            var dc = _self.GetSceneChild<DataTableComponent>();
            var rc = _self.GetSceneChild<ResourcesComponent>();
            var trial = _self.GetSceneChild<Trial>();
            if (dc == null || rc == null || trial == null)
            {
                return;
            }

            if (_isNew)
            {
                //加载图片
                for (int i = 0; i < trial.Trials.Count; i++)
                {
                    var trialData = dc.LoadDataTable<DRTrial>().GetData(trial.Trials[i].ID);
                    if (trialData != null)
                    {
                        //对应图片
                        _self.UIWindowBehaviour.GetButton($"Button_TrialItem{i}").Button.image = null;
                    }
                    else
                    {
                        //默认图片
                        _self.UIWindowBehaviour.GetButton($"Button_TrialItem{i}").Button.image = null;
                    }
                }
            }

            //更新状态
            for (int i = 0; i < trial.Trials.Count; i++)
            {
                var data = trial.Trials[i];
                _self.UpdateButtonState(i, data);
            }
        }

        public static void Close(this TrialUI _self)
        {
            if (_self.UIWindowBehaviour == null)
            {
                return;
            }

            _self.UIWindowBehaviour.Hide();
            _self.UIWindowBehaviour = null;
        }


        public static void Select(this TrialUI _self, int _id)
        {
            var trial = _self.GetSceneChild<Trial>();
            if (trial == null)
            {
                return;
            }

            trial.Execute(_id);
        }


        public static void UpdateButtonState(this TrialUI _self, int _index, TrialData _data)
        {
            var dc = _self.GetSceneChild<DataTableComponent>();
            var uc = _self.GetChild<UnityObjectComponent>();
            if (dc == null || uc == null)
            {
                return;
            }

            var button = _self.UIWindowBehaviour.GetButton($"Button_TrialItem{_index}");
            var tans = button.GetTransform();
            var nameText = uc.Get<TMP_Text>(tans.GetChild(0).gameObject);
            var coolingText = uc.Get<TMP_Text>(tans.GetChild(3).gameObject);
            nameText.text = dc.LoadDataTable<DRTrial>().GetData(_data.ID).Name;
            //0名字 1遮罩 2挑战图标 3倒计时图标 4完成图标 
            switch (_data.State)
            {
                case eTrialState.Ready:
                    button.Enable(true);
                    //隐藏不必要显示 
                    tans.GetChild(1).gameObject.SetActive(false);
                    tans.GetChild(2).gameObject.SetActive(false);
                    tans.GetChild(3).gameObject.SetActive(false);
                    tans.GetChild(4).gameObject.SetActive(false);
                    break;
                case eTrialState.Challenging:
                    button.Enable(false);
                    tans.GetChild(1).gameObject.SetActive(true);
                    tans.GetChild(2).gameObject.SetActive(true);
                    tans.GetChild(3).gameObject.SetActive(false);
                    tans.GetChild(4).gameObject.SetActive(false);
                    break;
                case eTrialState.Cooling:
                {
                    button.Enable(false);
                    tans.GetChild(1).gameObject.SetActive(true);
                    tans.GetChild(2).gameObject.SetActive(false);
                    tans.GetChild(3).gameObject.SetActive(true);
                    tans.GetChild(4).gameObject.SetActive(false);
                    if (_data.Cooldown == 0)
                    {
                        coolingText.text = "";
                    }
                    else
                    {
                        coolingText.text = ((int)_data.Cooldown + 1).ToString();
                    }
                }

                    break;
                case eTrialState.End:
                    button.Enable(false);
                    tans.GetChild(1).gameObject.SetActive(true);
                    tans.GetChild(2).gameObject.SetActive(false);
                    tans.GetChild(3).gameObject.SetActive(false);
                    tans.GetChild(4).gameObject.SetActive(true);
                    break;
            }
        }
    }
}